package com.bishe.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bishe.model.LoginModel;
import com.bishe.pojo.Student;
import com.bishe.query.StudentQuery;
import com.bishe.service.StudentService;
import com.bishe.util.EcoBootException;
import com.bishe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lwx
 * @since 2022-04-14
 */
@RestController
@CrossOrigin
@Api(tags = "学生控制器")
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @ApiOperation("教师登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginModel loginModel){
        LoginModel login = studentService.login(loginModel);

        if (login!=null){
            return Result.ok("ok",login);
        }
        return Result.error("用户名密码错误!");
    }

    @ApiOperation("学生列表")
    @GetMapping("/toPage")
    public Result toPage(StudentQuery query) {
        IPage iPage = studentService.toPage(query);
        return Result.ok(iPage);
    }

    @ApiOperation("添加或修改")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Student student) {

        if (StrUtil.isAllEmpty(student.getUserName(),student.getStudentNo())){
            throw new EcoBootException("信息不能为空");
        }
        studentService.addOrUpdate(student);
        return Result.ok("ok");
    }

    @ApiOperation("删除学生")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        studentService.removeById(id);
        return Result.ok();
    }

}

